//
//  MallViewController.m
//  GuoMei
//
//  Created by 王磊 on 2018/2/27.
//  Copyright © 2018年 王磊. All rights reserved.
//

#import "MallViewController.h"
#import "NavigationView.h"
#import "gridModel.h"
#import "MJExtension.h"
#import "GridCollectionViewCell.h"
@interface MallViewController()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) NSMutableArray<gridModel *> * gridModel;
@end
@implementation MallViewController
//-(UICollectionView *)collectionView{
//    if (!_collectionView) {
//        UICollectionViewLayout * collectionLayout = [[UICollectionViewLayout alloc]init];
//        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
//        _collectionView.frame = CGRectMake(0, 0, ScreenW, ScreenH);
//        //    _collectionView.backgroundColor = [UIColor redColor];
//        _collectionView.delaysContentTouches = NO;
//        _collectionView.showsVerticalScrollIndicator = NO;
//        _collectionView.delegate = self;
//        _collectionView.dataSource = self;
//        _collectionView.contentInsetAdjustmentBehavior = NO;
//        _collectionView.scrollEnabled = YES;
//        [_collectionView registerClass:[GridCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
//        [self.view addSubview:_collectionView];
//    }
//
//    return _collectionView;
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    [self obtainData];
    [self loadingCollectionView];
//    [self loadNavgationBar];
    
}
-(void)obtainData{
    _gridModel = [gridModel mj_objectArrayWithFilename:@"GoodsGrid.plist"];
    NSLog(@"%@",_gridModel);
}
-(void)loadNavgationBar{
    self.navigationController.navigationBar.hidden = YES;
    NavigationView * navView = [[NavigationView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 64)];
    navView.backgroundColor = [UIColor clearColor];
    navView.leftItemClickBlock = ^{
        NSLog(@"点击了扫一扫");
    };
    navView.rightItemClickBlock = ^{
        NSLog(@"点击了分类");
    };
    navView.secRightItemClickBlock = ^{
        NSLog(@"点击了购物车");
    };
    [self.view addSubview:navView];
}
-(void)loadingCollectionView{
    
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    UICollectionViewFlowLayout * collectionLayout = [[UICollectionViewFlowLayout alloc]init];
    collectionLayout.itemSize = CGSizeMake(self.view.frame.size.width/5 , self.view.frame.size.width/5 + 10);
    collectionLayout.minimumLineSpacing = 0;
    collectionLayout.minimumInteritemSpacing = 0;
//    collectionLayout.maximumSpacing = 0;/  collectionLayout.sectionInset = UIEdgeInsetsMake(0, 4.9, 0, 4.9);
    
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
    _collectionView.frame = CGRectMake(0, 0, ScreenW, ScreenH);

//    _collectionView.backgroundColor = [UIColor redColor];
    _collectionView.delaysContentTouches = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentInsetAdjustmentBehavior = NO;
    _collectionView.scrollEnabled = YES;
    [_collectionView registerClass:[GridCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.view addSubview:_collectionView];
  
   
}
#pragma mark collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _gridModel.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GridCollectionViewCell *cell = (GridCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    cell.gridItem = _gridModel[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

  return CGSizeMake(self.view.frame.size.width/5 , self.view.frame.size.width/5 + 10);

}


@end
